﻿/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />
/// <reference path="AjaxControlToolkit.Common.Common.js" />

Type.registerNamespace('CARMS');
CARMS.PopupEditorBehavior = function(element) {
    CARMS.PopupEditorBehavior.initializeBase(this, [element]);
    this._value = null;
    this._textElement = null;
    this._buttonElement = null;
    this._dialogUrl = null;

    this._errorMessage = null;
    this._enabled = true;

    this._buttnClickHandler = null;
    this._textChangedHandler = null;
}
CARMS.PopupEditorBehavior.prototype = {
    initialize: function() {
        CARMS.PopupEditorBehavior.callBaseMethod(this, 'initialize');
        this._invalidate();
        this.get_element().focus = Function.createDelegate(this, this._focus)
    },
    dispose: function() {
        if (this._buttnClickHandler) {
            $removeHandler(this._buttonElement, 'click', this._buttnClickHandler);
            this._buttnClickHandler = null;
        }
        if (this._textChangedHandler) {
            $removeHandler(this._textElement, 'change', this._textChangedHandler);
            this._textChangedHandler = null;
        }
        CARMS.PopupEditorBehavior.callBaseMethod(this, 'dispose');
    },
    _focus: function() {
        if (this._enabled) {
            this._textElement.focus();
        }
    },
    _onButtonClick: function(evt) {
    $dialog(this._dialogUrl, { width: 700, height: 550 }, Function.createDelegate(this, this._onDialogClosed));
        evt.preventDefault();
    },
    _onDialogClosed: function(result) {
    if (result) {
            this._value = result[0];
            if (this.get_element() != null) {
                this.get_element().value = this._value;
                $text(this._textElement, result[1]);
                this.raiseValueChanged(result);
            }
        }
    },
    _onTextChanged: function(evt) {
    
        this._invoke('GetValue', [$text(this._textElement)], Function.createDelegate(this, this._textChangeCallback));
    },
    _textChangeCallback: function(result) {
        if (result) {
            this._value = result;
            this.get_element().value = this._value;
            this.raiseValueChanged(result);
        } else {
            $alert(this._errorMessage, Function.createDelegate(this, function() {
                this._value = "";
                this.get_element().value = "";
                $text(this._textElement, "");
                this.raiseValueChanged(null);

            }))
        }
    },
    _invalidate: function() {
        if (this._enabled) {

            this._textElement.disabled = false;
            $common.setVisible(this._buttonElement, true);
            this._buttnClickHandler = Function.createDelegate(this, this._onButtonClick);
            $addHandler(this._buttonElement, 'click', this._buttnClickHandler);
            this._textChangedHandler = Function.createDelegate(this, this._onTextChanged);
            $addHandler(this._textElement, 'change', this._textChangedHandler);
        } else {
            this._textElement.disabled = true;
            $common.setVisible(this._buttonElement, false);



            if (this._buttnClickHandler) {
                $removeHandler(this._buttonElement, 'click', this._buttnClickHandler);
                this._buttnClickHandler = null;
            }
            if (this._textChangedHandler) {
                $removeHandler(this._textElement, 'change', this._textChangedHandler);
                this._textChangedHandler = null;
            }
        }
    },
    loadClientState: function(value) {
        var state = Sys.Serialization.JavaScriptSerializer.deserialize(value);
        this._value = state.Value;
        this.get_element().value = this._value;
        this._textElement = $get(state.TextElementID);
        this._buttonElement = $get(state.ButtonElementID);
        this._dialogUrl = state.DialogUrl;
    },
    saveClientState: function() {
        return this._value;
    },
    add_valueChanged: function(handler) {
        this.get_events().addHandler('ValueChanged', handler);
    },
    remove_valueChanged: function(handler) {
        this.get_events().removeHandler('ValueChanged', handler);
    },
    raiseValueChanged: function(eventArgs) {
    
        var handler = this.get_events().getHandler('ValueChanged');
        if (handler) {
            handler(this, eventArgs);
        }
    },
    get_errorMessage: function() {
        return this._errorMessage;
    },
    set_errorMessage: function(value) {
        if (this._errorMessage !== value) {
            this._errorMessage = value;
            if (this.get_isInitialized()) {
                this.raisePropertyChanged("errorMessage");
            }
        }
    },
    get_value: function() {
        return this._value;
    },

    get_enabled: function() {
        return this._enabled;
    },
    set_enabled: function(value) {

        if (this._enabled !== value) {
            this._enabled = value;
            if (this.get_isInitialized()) {
                this._invalidate();
                this.raisePropertyChanged("enabled");
            }
        }
    },
    set_TextValue: function(value) {
    
        $text(this._textElement, value);
        this._invoke('GetValue', [$text(this._textElement)], Function.createDelegate(this, this._textChangeCallback));

    }
}
CARMS.PopupEditorBehavior.registerClass('CARMS.PopupEditorBehavior', AjaxControlToolkit.ControlBase);
